package com.ruoyi.project.system.controller;

import java.util.List;

import com.ruoyi.project.system.domain.SysAddress;
import com.ruoyi.project.system.domain.SysCity;
import com.ruoyi.project.system.service.ISysAddressService;
import com.ruoyi.project.system.service.ISysCityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.service.ISysMobileUserService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 用户端appController
 *
 * @author jjp
 * @date 2020-04-13
 */
@RestController
@RequestMapping("/system/mobileUser")
public class SysMobileUserController extends BaseController {
    @Autowired
    private ISysMobileUserService sysMobileUserService;
    @Autowired
    private ISysAddressService sysAddressService;
    @Autowired
    private ISysCityService sysCityService;

    /**
     * 查询用户端app列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysMobileUser sysMobileUser) {

        startPage();
        List<SysMobileUser> list = sysMobileUserService.selectSysMobileUserList(sysMobileUser);
        return getDataTable(list);
    }


    /**
     * 获取用户端app详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = "/{mobileUserId}")
    public AjaxResult getInfo(@PathVariable("mobileUserId") Long mobileUserId) {
        SysMobileUser sysMobileUser = sysMobileUserService.selectSysMobileUserById(mobileUserId);
        List<SysAddress> list = sysAddressService.selectSysAddressAllList(mobileUserId);
        for (int i = 0; i < list.size(); i++) {
            Long area = Long.valueOf(list.get(i).getArea());
            SysCity sysCity1 = sysCityService.selectSysCityById(area);
            //市名称
            String cityName1 = sysCity1.getCityName();
            //省名称
            String cityName2 = null;
            if (sysCity1.getParentCityId() != null) {
                SysCity sysCity2 = sysCityService.selectSysCityById(sysCity1.getParentCityId());
                cityName2 = sysCity2.getCityName();
            }
            String cityName = cityName2 + cityName1;
            list.get(i).setArea(cityName);
        }
        sysMobileUser.setSysAddresses(list);
        return AjaxResult.success(sysMobileUser);
    }

    /**
     * 新增用户端app
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户端app", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMobileUser sysMobileUser) {
        return toAjax(sysMobileUserService.insertSysMobileUser(sysMobileUser));
    }

    /**
     * 修改用户端app
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户端app", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMobileUser sysMobileUser) {
        return toAjax(sysMobileUserService.updateSysMobileUser(sysMobileUser));
    }

    /**
     * 删除用户端app
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户端app", businessType = BusinessType.DELETE)
    @DeleteMapping("/{mobileUserIds}")
    public AjaxResult remove(@PathVariable Long[] mobileUserIds) {
        return toAjax(sysMobileUserService.deleteSysMobileUserByIds(mobileUserIds));
    }
}
